<template>
  <div class="container">
    <div class="bg"></div>
    <van-nav-bar title="爱阅之城" class="navbar">
      <template #left>
        <van-icon
          name="arrow-left"
          class="icon"
          size="20"
          @click="onClickLeft"
        />
      </template>
    </van-nav-bar>
    <!-- 用户登录 -->
    <div class="login" v-show="isShow">
      <div class="tilte">欢迎登录</div>
      <div class="item item1">
        <div class="label">账号</div>
        <div class="data">
          <input
            type="text"
            placeholder="请输入手机号码"
            name="phone"
            v-model="loginData.phone"
          />
        </div>
      </div>
      <div class="item">
        <div class="label">密码</div>
        <div class="data">
          <input
            type="password"
            placeholder="请输入密码"
            name="password"
            v-model="loginData.password"
          />
        </div>
      </div>
      <div class="btn">
        <van-button color="#5a78ec" round class="button" block @click="toLogin"
          >登录</van-button
        >
      </div>
      <div class="toRegister" @click="changePage">
        <img src="../../assets/img/toRegister.png" alt="" />
      </div>
    </div>
    <div class="loginBg" v-show="isShow">
      <img src="../../assets/img/login_bg.png" alt="" />
    </div>
    <!-- 用户注册 -->
    <div class="register" v-show="!isShow">
      <div class="tilte">用户注册</div>
      <div class="item item1">
        <div class="label">姓名</div>
        <div class="data">
          <input
            type="text"
            placeholder="请输入姓名"
            name="username"
            v-model="registerData.username"
          />
        </div>
      </div>
      <div class="item">
        <div class="label">账号</div>
        <div class="data">
          <input
            type="text"
            placeholder="请输入手机号码"
            name="phone"
            v-model="registerData.phone"
          />
        </div>
      </div>
      <div class="item">
        <div class="label">密码</div>
        <div class="data">
          <input
            type="password"
            placeholder="请输入密码"
            name="password"
            v-model="registerData.password"
          />
        </div>
      </div>
      <div class="item">
        <div class="label">确认密码</div>
        <div class="data">
          <input
            type="password"
            placeholder="请确认密码"
            name="password2"
            v-model="registerData.password2"
          />
        </div>
      </div>
      <div class="btn">
        <van-button
          color="#5a78ec"
          round
          class="button"
          block
          @click="toRegister"
          >注册</van-button
        >
      </div>
      <div class="toRegister" @click="changePage">
        <img src="../../assets/img/toLogin.png" alt="" />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isShow: true,
      //登录用户数据
      loginData: {
        phone: "",
        password: "",
      },
      //注册用户数据
      registerData: {
        username: "",
        phone: "",
        password: "",
        password2: "",
      },
    };
  },
  methods: {
    //登录注册切换逻辑
    changePage() {
      this.isShow = !this.isShow;
    },
    //用户登录逻辑
    async toLogin() {
      let { status, message, token, userInfo } = await this.$http({
        url: "/login",
        method: "post",
        data: {
          ...this.loginData,
        },
      });
      if (status !== 200) {
        this.$toast.fail({
          message: message,
          position: "center",
        });
        return;
      }
      this.$store.commit("setToken", { token });
      this.$store.commit("setUserInfo", { userInfo });
      this.$router.back();
      this.$toast.success({
        message: message,
        position: "center",
      });
      return;
    },
    //用户注册逻辑
    async toRegister() {
      let { password, password2 } = this.registerData;
      if (password !== password2) {
        this.$toast.fail({
          message: "两次输入的密码不相同",
          position: "center",
        });
        return;
      }
      let { status, message } = await this.$http({
        url: "/users/register",
        method: "post",
        data: {
          ...this.registerData,
        },
      });
      if (status !== 200) {
        this.$toast.fail({
          message,
          position: "center",
        });
        return;
      }
      this.isShow = true;
      this.$toast.success({
        message,
        position: "center",
      });
    },
    onClickLeft() {
      this.$router.back();
    },
  },
};
</script>

<style scoped>
input {
  color: #646468;
}
.icon {
  color: #808080;
}
.bg {
  background-color: #ecebeb;
  width: 100vw;
  height: 100vh;
  position: fixed;
  left: 0;
  top: 0;
  z-index: -1;
}
.login {
  background-color: #ffffff;
  border-radius: 1rem;
  position: fixed;
  left: 50%;
  top: 50%;
  width: 87%;
  transform: translate(-50%, -50%);
}
.register {
  background-color: #ffffff;
  border-radius: 1rem;
  position: fixed;
  left: 50%;
  top: 50%;
  width: 87%;
  transform: translate(-50%, -50%);
}
.tilte {
  text-align: center;
  color: #6d88ee;
  font-size: 1.3rem;
  font-weight: 600;
  margin-top: 1.5rem;
}
.item {
  margin: 0 auto;
  width: 90%;
}
.item1 {
  margin-top: 3rem;
}
.item .label {
  color: #5a78ec;
  font-weight: 550;
  font-size: 0.6rem;
  margin: 1rem 0;
}

.item .data input {
  border: 0.0625rem solid #afafaf;
  border-radius: 0.625rem;
  padding: 0.3125rem;
  height: 2.5rem;
  box-sizing: border-box;
  width: 100%;
}
.btn {
  display: flex;
  justify-content: center;
  margin: 2.2rem 0;
}
.button {
  width: 35%;
  font-size: 1rem;
  border-radius: 1rem;
}
.loginBg {
  position: fixed;
  bottom: 2%;
  right: 5%;
}
.toRegister {
  position: absolute;
  right: 2%;
  top: 3%;
}
.toRegister img {
  width: 2.5rem;
  height: 2.5rem;
}
</style>